#include <cstdio>

long long josephus0(long long n, long long m)
{
    if (n == 2) return (m%2) ? 2 : 1;
    long long v = (m+josephus0(n-1,m)) % n;
    if (v == 0) v = n;
    return v;
}
long long josephus(long long n, long long m)
{
    if (m == 1) return n;
    if (n == 1) return 1;
    if (m >=n) return josephus0(n,m);
    long long l = (n/m)*m;
    long long j = josephus(n - (n/m), m);
    if (j <= n-l) return l+j;
    j -= n-l;
    long long t = (j/(m-1))*m;
    if ((j % (m-1)) == 0) return t-1;
    return t + (j % (m-1));
}

int main( )
{
    long long n, k;
    while ( scanf("%I64d%I64d", &n, &k) != EOF )
        printf("%I64d\n", josephus( n, k ));
    return 0;
}
